﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="PayOperation.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：PayOperation.cs
// 创建时间：2015-01-08 10:18
// 修改时间：2015-01-08 10:18
// </summary>
// ---------------------------------------------------------------------------------------
#endregion
namespace Payment.ChinaPay
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class PayOperation
    {
        /// <summary>
        ///     The pay.
        /// </summary>
        protected NetPay Pay;

        /// <summary>
        ///     The Service.
        /// </summary>
        internal readonly PayService Service;

        /// <summary>
        /// The config.
        /// </summary>
        private readonly ChinaPayConfig config;

        /// <summary>
        /// 是否创建成功
        /// </summary>
        public bool IsbuildKey
        {
            get
            {
                return this.Pay.PrivateKeyFlag;
            }
        }

        /// <summary>
        /// 银联业务服务
        /// </summary>
        /// <param name="service">
        /// </param>
        /// <param name="config"></param>
        public PayOperation(PayService service, ChinaPayConfig config)
        {
            this.Service = service;
            this.Pay = new NetPay();
            this.Service.MerId = config.MerId;
            this.config = config;
            var buildKey = this.Pay.buildKey(config.MerId, config.KeyUsage, config.KeyFile);
            if (!buildKey)
            {
                throw new Exception("密钥路径错误");
            }
        }

        /// <summary>
        /// 是否答应发送
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool IsTransResponse()
        {
            return this.Pay.verifyTransResponse(
                this.Service.MerId,
                this.Service.OrdId,
                this.Service.TransAmt,
                this.Service.CuryId,
                this.Service.TransDate,
                this.Service.TransType,
                "0001",
                this.Sign());
        }


        /// <summary>
        /// 进行签名
        /// </summary>
        /// <returns></returns>
        public virtual string Sign()
        {
            var plainData = this.Service.MerId + this.Service.OrdId + this.Service.TransAmt + this.Service.CuryId
                            + this.Service.TransDate + this.Service.TransType + this.Service.Remarks;
            if (this.Pay.PrivateKeyFlag)
            {
                var chkValue2 = this.Pay.Sign(plainData);
                return chkValue2;

            }
            else
            {
                throw new Exception("进行签名失败");
            }
        }

        public string PlainData { get; private set; }

        /// <summary>
        /// 分账签名 MerId+ BusiId+ OrdId+ OrdAmt+ CuryId+ Version+ BgRetUrl+ PageRetUrl+ GateId+ Param1N+ ShareType+ShareData+ Priv1+CustomIp
        /// </summary>
        /// <returns></returns>
        public string Sign(Dictionary<string, string> signDictionary)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var item in signDictionary)
            {
                sb.Append(item.Value);
            }

            this.PlainData = sb.ToString();
            this.PlainDataB64 = this.B64(sb.ToString());
            if (this.Pay.PrivateKeyFlag)
            {
                return this.Pay.Sign(this.PlainDataB64);
            }
            else
            {
                throw new Exception("进行签名失败");
            }
        }


        public string PlainDataB64 { get; private set; }

        string B64(string str)
        {
            string encode;
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = str;
            }
            return encode;
            //byte[] bytes = Encoding.Default.GetBytes(str,);
            //return Convert.ToBase64String(bytes); 
        }

    }
}